package cn.edu.scau.cmi.dengzejun.chapter11;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;
import java.io.*;

public class WindowItemEvent extends JFrame{
	JComboBox choice;
	JTextArea textShow;
	PoliceListenForSeven listen;
	public WindowItemEvent() {
		this.init();
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		return;
	}
	
	private void init() {
		this.setLayout(new FlowLayout());
		this.choice = new JComboBox();
		this.choice.addItem("Please choose the File");
		File dir = new File(".");
		FileAccept fileAccept = new FileAccept();
		fileAccept.setExtendName("java");
		String[] fileName = dir.list(fileAccept);
		for (String name:fileName) {
			this.choice.addItem(name);
		}
		this.textShow = new JTextArea(9,30);
		this.listen = new PoliceListenForSeven();
		this.listen.setJComboBox(this.choice);
		this.listen.setJTextArea(this.textShow);
		this.choice.addItemListener(this.listen);
		this.add(this.choice);
		this.add(new JScrollPane(this.textShow));
		return;
	}
	
	class FileAccept implements FilenameFilter {
		private String extendName;
		public void setExtendName(String s) {
			this.extendName = "."+s;
			return;
		}
		
		@Override
		public boolean accept(File dir, String name) {
			// TODO Auto-generated method stub
			return name.endsWith(this.extendName);
		}
	}
}

